<template>
  <el-dialog v-bind="$attrs" v-on="$listeners" @open="onOpen" @close="onClose" title="Dialog Titile">
    <el-row :gutter="32">
      <el-form ref="字典添加" :model="formData" :rules="rules" size="medium" label-width="100px"
               label-position="left">
        <el-col :span="20">
          <el-form-item label="权限名称" prop="permissionsName">
            <el-input v-model="formData.permissionsName" placeholder="请输入权限名称" clearable
                      :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="访问路径" prop="interfaceUrl">
            <el-input v-model="formData.interfaceUrl" placeholder="请输入访问路径" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="20">
          <el-form-item label="所属角色" prop="role">
            <el-select v-model="formData.role.roleId" placeholder="请选择所属角色"  :style="{width: '100%'}">
              <el-option v-for="(item, index) in roleIdOptions" :key="index" :label="item.roleName"
                         :value="item.roleId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="handelConfirm">确定</el-button>
    </div>
  </el-dialog>
</template>

<style>

</style>

<script>
import axios from "axios"
import Cookie from "js-cookie"
export default {
  inheritAttrs: false,
  components: {},
  props: ["init","closeAdd","admin"],
  data() {
    return {
      formData: {
        permissionsName: undefined,
        interfaceUrl: undefined,
        role:{
          roleId: undefined
        }
      },
      rules: {
        permissionsName: [{
          required: true,
          message: '请输入权限名称',
          trigger: 'blur'
        }],
        interfaceUrl: [{
          required: true,
          message: '请输入访问路径',
          trigger: 'blur'
        }],
        role: [{
          required: true,
          message: '请选择所属角色',
          trigger: 'change'
        }],
      },
      roleIdOptions: []
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    console.log(this.admin.adminId)
    var th=this;
    axios.get("http://localhost:8001/role").then((data)=>{
      console.log(data.data)
      th.roleIdOptions=data.data.records;
      console.log(th.roleIdOptions);
    })
  },
  methods: {
    onOpen() {},
    onClose() {
      this.$refs['字典添加'].resetFields()
    },
    close() {
      this.$emit('update:visible', false)
    },
    handelConfirm() {

      this.$refs['字典添加'].validate(valid => {
        if (!valid) return
        axios.post("http://localhost:8001/Permissions/insertPermissions", {
          permissionsName: this.formData.permissionsName,
          interfaceUrl: this.formData.interfaceUrl,
          role:{
            roleId: this.formData.role.roleId
          },
          creatorId:this.admin.adminId,
          updateBy:this.admin.adminId
        }).then((data)=>{
          console.log(data);
          axios.post("http://localhost:8001/RolePermission",{
            roleId:this.formData.role.roleId,
            permissionsId:data.data,
            createBy:this.admin.adminId,
            updateBy:this.admin.adminId
          }).then((data)=>{
            this.$message({
              showClose: true,
              message: '添加成功',
              type: 'success'
            });
            this.init();
            this.closeAdd();
            this.close()
          })
        }).catch((data)=>{
          this.$message({
            showClose: true,
            message: '未知原因,请联系管理员',
            type: 'error'
          });
        });
        this.init();
        this.closeAdd();
        this.close()
      })
    },
  }
}

</script>
